import requests
from bs4 import BeautifulSoup
import xlwt


def slove(firstGood):
    picPath = firstGood.find('div', class_='pic-box SP')['data-rel']
    print(picPath)
    name = firstGood.find('div', class_='pro-intro').find('h3').a.get_text()
    print(name)
    infos = firstGood.find('ul', class_='param clearfix').find_all('li')
    for info in infos:
        print(info.get_text())
    price = firstGood.find('div', class_='price-box').find('b', class_='price-type').get_text()
    print(price)
    print("-------------------------")


def get_html(url):
    # 模拟浏览器访问
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54',
        'accept-language': 'zh-CN,zh;q=0.9'
    }
    print("--> 正在获取网站信息")
    response = requests.get(url, headers=headers)  # 请求访问网站
    if response.status_code == 200:
        html = response.text  # 获取网页源码
        return html  # 返回网页源码
    else:
        print("获取网站信息失败！")


if __name__ == '__main__':
    # 搜索地址
    search_url = 'https://detail.zol.com.cn/cpu/amd/new_pic.html'
    html = get_html(search_url)
    soup = BeautifulSoup(html, 'lxml')
    firstGood = soup.find('div', class_='list-item item-one clearfix')
    goodsList = soup.find_all('div', class_='list-item clearfix')
    print(slove(firstGood))
    for li in goodsList:
        print(slove(li))
